home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / music_utilities / pt012.dms / pt012.adf / NoDelete / src / nodelete201.c < prev    next >
C/C++ Source or Header  |  1992-05-14  |  13KB  |  615 lines

  1. /* NoDelete V2.01 (C) HooverSoft 1992
  2.  
  3.     cc NoDelete +L
  4.     ln NoDelete -la -lc32
  5.  
  6.  
  7. */
  8.  
  9.  
  10. #ifdef AZTEC_C                    /* Works under Manx, convert it to Lattice */
  11.                                     /* if you don't mind the hassle...             */
  12.  
  13. #include<libraries/arpbase.h>
  14. #include<intuition/intuition.h>
  15. #include<intuition/intuitionbase.h>
  16. #include<libraries/dos.h>
  17. #include<libraries/dosextens.h>
  18. #include<stdio.h>
  19.  
  20. /*#define DEBUG 1*/
  21.  
  22. void *AllocMem();
  23. char bodystring[80], *patterns[50], titlebak[80], title[80];
  24. ULONG answer, PAT_SET = 0L,  howmany = 0L, rvalue=0L;
  25. ULONG OVERRIDE = 0L, SILENT = 0L, deletefile;
  26. ULONG IS2_0=0; /* 1.2 & 1.3 still more common than 2.0 */
  27. ULONG ACTIVE;
  28. char *what=(char *)"NoDelete V2.01 © HooverSoft 1992";
  29. struct NewWindow mywindow = {
  30.     100,0,    /* window XY origin relative to TopLeft of screen */
  31.     350,10,    /* window width and height */
  32.     0,1,    /* detail and block pens */
  33.     CLOSEWINDOW+VANILLAKEY,    /* IDCMP flags */
  34.     WINDOWDRAG+WINDOWDEPTH+WINDOWCLOSE+SIMPLE_REFRESH,    /* other window flags */
  35.     NULL,    /* first gadget in gadget list */
  36.     NULL,    /* custom CHECKMARK imagery */
  37.     (UBYTE *)title,    /* window title */
  38.     NULL,    /* custom screen pointer */
  39.     NULL,    /* custom bitmap */
  40.     5,5,    /* minimum width and height */
  41.     640,110, /* maximum width and height */
  42.     WBENCHSCREEN    /* destination screen type */
  43.     };
  44.  
  45.  
  46.  
  47. char *patgadsibuff;
  48. char *undobuff;
  49.  
  50. struct StringInfo patgadsinfo = {
  51.     (UBYTE *) NULL,    /* buffer where text will be edited */
  52.     (UBYTE *) NULL,    /* optional undo buffer */
  53.     0,    /* character position in buffer */
  54.     255,    /* maximum number of characters to allow */
  55.     0,    /* first displayed character buffer position */
  56.     0,0,0,0,0,    /* Intuition initialized and maintained variables */
  57.     0,    /* Rastport of gadget */
  58.     0,    /* initial value for integer gadgets */
  59.     NULL    /* alternate keymap (fill in if you set the flag) */
  60. };
  61.  
  62. SHORT BorderVectors1[] = {
  63.     0,0,
  64.     179,0,
  65.     179,9,
  66.     0,9,
  67.     0,0
  68. };
  69. struct Border Border1 = {
  70.     -1,-1,    /* XY origin relative to container TopLeft */
  71.     3,0,JAM1,    /* front pen, back pen and drawmode */
  72.     5,    /* number of XY vectors */
  73.     BorderVectors1,    /* pointer to XY vectors */
  74.     NULL    /* next border in list */
  75. };
  76.  
  77. struct Gadget patgad = {
  78.     NULL,    /* next gadget */
  79.     4,11,    /* origin XY of hit box relative to window TopLeft */
  80.     178,8,    /* hit box width and height */
  81.     NULL,    /* gadget flags */
  82.     RELVERIFY,    /* activation flags */
  83.     STRGADGET,    /* gadget type flags */
  84.     (APTR)&Border1,    /* gadget border or image to be rendered */
  85.     NULL,    /* alternate imagery for selection */
  86.     NULL,    /* first IntuiText structure */
  87.     NULL,    /* gadget mutual-exclude long word */
  88.     (APTR)&patgadsinfo,    /* SpecialInfo structure */
  89.     42L,    /* user-definable data */
  90.     NULL    /* pointer to user-definable data */
  91. };
  92.  
  93. #define GadgetList1 patgad
  94.  
  95. struct NewWindow patwindow = {
  96.     214,2,    /* window XY origin relative to TopLeft of screen */
  97.     187,22,    /* window width and height */
  98.     0,1,    /* detail and block pens */
  99.     CLOSEWINDOW|GADGETUP,    /* IDCMP flags */
  100.     WINDOWDRAG+WINDOWCLOSE+ACTIVATE+NOCAREREFRESH,    /* other window flags */
  101.     &patgad,    /* first gadget in gadget list */
  102.     NULL,    /* custom CHECKMARK imagery */
  103.     (UBYTE *)"Edit Patterns",    /* window title */
  104.     NULL,    /* custom screen pointer */
  105.     NULL,    /* custom bitmap */
  106.     5,5,    /* minimum width and height */
  107.     -1,-1,    /* maximum width and height */
  108.     WBENCHSCREEN    /* destination screen type */
  109. };
  110.  
  111. struct IntuiText body = {
  112.     1,0,
  113.     JAM1|COMPLEMENT,
  114.     30,16,
  115.     NULL,
  116.     NULL,
  117.     NULL
  118.     };
  119.  
  120. struct IntuiText fbody = {
  121.     1,0,
  122.     JAM1|COMPLEMENT,
  123.     30,5,
  124.     NULL,
  125.     (UBYTE *) "System tries to fool around...",
  126.     &body
  127.     };
  128.  
  129. struct IntuiText ok = {
  130.     1,0,
  131.     JAM1|COMPLEMENT,
  132.     6,3,
  133.     NULL,
  134.     (UBYTE *)    "Yes, Delete!",
  135.     NULL
  136.     };
  137.  
  138. struct IntuiText no = {
  139.     1,0,
  140.     JAM1|COMPLEMENT,
  141.     6,3,
  142.     NULL,
  143.     (UBYTE *)  "Oops, No Way!",
  144.     NULL
  145.     };
  146.  
  147.  
  148. struct IntuiMessage *msg, *GetMsg();
  149. struct MsgPort *FindPort(), *CreatePort(), *nd_msgport;
  150. struct IntuitionBase *IntuitionBase;
  151. struct Window            *pw, *mw, *aw, *OpenWindow();
  152. struct Library            *DosBase;
  153. struct ArpBase            *ArpBase;
  154. UBYTE                        *filename;
  155. void                         *OpenLibrary();
  156. struct Task                *FindTask();
  157.  
  158. #ifdef DEBUG
  159. void print_info()
  160. {
  161. long i;
  162. for(i=0; i<howmany; i++)
  163.     Printf("Pattern %ld: %s\n", i, patterns[i]);
  164. return;
  165. }
  166. #endif
  167. /* Now here comes the heart of NoDelete. Please excuse the crummy
  168.     structure of the code, but this is another example of a program
  169.     thas has "grown" naturally. I only understand half of this myself!
  170. */
  171.  
  172. void NoDelete()
  173. {
  174.  
  175.     char parsed[131]; /* place to keep the preparsed string */
  176.  
  177. #asm
  178.     public    _LVOSetProtection
  179.     public   _LVOSetFunction
  180.  
  181.     movem.l    d0-d7/a0-a6,-(sp)
  182. #endasm
  183.     geta4(); /* THIS is very important due to Aztec's a4-relative addressing */
  184. #asm
  185.     move.l    d1,_filename
  186. #endasm
  187.     ;
  188.  
  189.     ;
  190.     ACTIVE=1;
  191.     if(PAT_SET)
  192.         {
  193.  
  194.         for(answer=0L; answer < howmany; answer++)
  195.             {
  196.             PreParse(patterns[answer], parsed);   /* This is ARP */
  197.             if( !PatternMatch(parsed, filename) ) /* this is new */
  198.                 continue;
  199.             else
  200.                 goto request;
  201.             }
  202.         }
  203.     else
  204.         goto request;
  205.  
  206.     goto delete_anyway;
  207.  
  208. request:
  209.     strcpy(bodystring, "Delete File `");
  210.     strcat(bodystring, (char *)BaseName(filename) );
  211.     strcat(bodystring, "'?");
  212.  
  213.     body.IText = (UBYTE *) bodystring;
  214.     aw = (struct Window *) IntuitionBase->ActiveWindow;
  215.     answer = (long)AutoRequest(aw, &fbody, &ok, &no, NULL, NULL, 360L, 61L);
  216.     if(!answer)
  217.     {
  218.         ;
  219.     ACTIVE=0;
  220. #asm
  221.     movem.l    (sp)+,d0-d7/a0-a6
  222.     move.l    #1,d0 ;This is where we fool the calling program into believing
  223.                   ;that everything is removed.
  224. #endasm
  225.     ;
  226.     return;
  227.     }
  228.  
  229.     if(answer)
  230.     {
  231.  
  232. delete_anyway:
  233.         ;
  234.  
  235. #asm
  236.         tst.l   _IS2_0
  237.         bne.s  _is20
  238.         move.l    _DosBase,a6
  239.         move.l    #$70f7,d0            ;restore old vector
  240.         move.w    d0,-72(a6)
  241.         move.l    #$60000092,d0
  242.         move.l    d0,-70(a6)
  243.         bra.s    _not20
  244.  
  245. _is20:
  246.         move.l 4,a6
  247.         move.l _DosBase,a1
  248.         move.l #-72,a0
  249.         move.l _deletefile,d0
  250.         jsr    _LVOSetFunction(a6)
  251.       move.l _DosBase,a6
  252.  
  253. _not20:
  254.         tst.l        _OVERRIDE   /* see if global OVERRIDE equals zero */
  255.         beq.s        _dont_unprotect
  256.  
  257.         move.l    _filename,d1
  258.  
  259.  
  260.         move.l    #0,d2
  261.         jsr        _LVOSetProtection(a6)
  262.  
  263. _dont_unprotect:
  264.  
  265.         move.l    _filename,d1
  266.         jsr        -72(a6)                ;DeleteFile() call
  267.         move.l   d0,_rvalue        ;save the return value
  268. #endasm
  269.  
  270.         ;
  271.  
  272.         SetFunction(DosBase, -72L, &NoDelete);
  273.       ACTIVE=0;
  274. /* install our function */
  275.  
  276.  
  277. #asm
  278.         movem.l    (sp)+,d0-d7/a0-a6
  279.         move.l    _rvalue,d0
  280. #endasm
  281.         ;
  282.         return;
  283.         }
  284.     }
  285.  
  286.  
  287. void quit(why)
  288. UBYTE *why;
  289. {
  290. long i;
  291.  
  292.     if(!SILENT)
  293.         Printf(why);
  294.     SetTaskPri((struct Task *)FindTask(0L), 0L);
  295.     if(DosBase)
  296.     {
  297.         ;
  298.         if(!IS2_0)
  299.             {
  300.             ;
  301.  
  302. #asm
  303.         move.l    _DosBase,a6
  304.         move.l    #$70f7,d0            ;restore old vector
  305.         move.w    d0,-72(a6)
  306.         move.l    #$60000092,d0
  307.         move.l    d0,-70(a6)
  308. #endasm
  309.             ;
  310.             }
  311.  
  312.         else  /* setfunction should perform as advertised with 2.0 */
  313.             SetFunction(DosBase, -72L, deletefile);
  314.  
  315.     }
  316.  
  317.     if (nd_msgport)
  318.         DeletePort(nd_msgport);
  319.  
  320.     if    (mw)
  321.         CloseWindowSafely(mw);
  322.  
  323.     if(ArpBase)
  324.         CloseLibrary(ArpBase);
  325.  
  326.     for(i=0; i<howmany; i++)
  327.         FreeMem(patterns[i], strlen(patterns[i])+1L);
  328.     exit(0L);
  329. }
  330.  
  331. void edit_patterns()
  332. {
  333.     static struct IntuiMessage *pwmsg=0L;
  334.     long pwclass, pwcode, i;
  335.     char *tmp;
  336.  
  337.     patgadsibuff = (char *) AllocMem(512L, MEMF_CLEAR);
  338.         if(!patgadsibuff)
  339.             quit("Could not allocate buffer for stringadget (512 bytes)!");
  340.         else
  341.             patgadsinfo.Buffer = (UBYTE *)patgadsibuff;
  342.  
  343.    undobuff = (char *) AllocMem(512L, MEMF_CLEAR);
  344.  
  345.         if(!undobuff)
  346.             patgadsinfo.UndoBuffer = NULL;
  347.       else
  348.             patgadsinfo.UndoBuffer = (UBYTE *) undobuff;
  349.  
  350.  
  351.     if(howmany)
  352.         {
  353.         *patgadsibuff=0;
  354.         for(i=0; i<howmany; i++)
  355.             {
  356.             strcat(patgadsibuff, patterns[i]);
  357.             strcat(patgadsibuff, " ");
  358.             }
  359.         patgadsibuff[strlen(patgadsibuff)-1] = 0; /* take care of extra blank*/
  360.  
  361.         }
  362.     pw = (struct Window *) OpenWindow(&patwindow);
  363.     ActivateGadget(&patgad, pw, NULL);
  364.     for(;;)
  365.         {
  366.         Wait(1L << pw -> UserPort -> mp_SigBit);
  367.       while( (pwmsg = GetMsg(pw -> UserPort) ) )
  368.             {
  369.          pwclass = pwmsg -> Class;
  370.          pwcode = pwmsg -> Code;
  371.             ReplyMsg(pwmsg);
  372.  
  373.             switch(pwclass)
  374.                 {
  375.                 case CLOSEWINDOW    :     goto bye;
  376.                 break;
  377.  
  378.                 case GADGETUP:
  379. #ifdef DEBUG
  380.                 Printf("String entered is %s!\n", patgadsibuff);
  381. #endif
  382.             tmp=patgadsibuff;
  383.                 split_string(tmp);
  384.  
  385.                 goto bye;
  386.                 break;
  387.  
  388.                 case default:
  389.                 break;
  390.                 }
  391.             }
  392.         }
  393.  
  394.  
  395. bye:
  396.     if(patgadsibuff)
  397.         FreeMem(patgadsibuff, 512L);
  398.     if(undobuff)
  399.         FreeMem(undobuff, 512L);
  400.     if(pw)
  401.         CloseWindowSafely(pw);
  402. return;
  403. }
  404.  
  405.  
  406.  
  407. void main(argc,argv)
  408. long argc;
  409. char *argv[];
  410.     {
  411.  
  412.     static UBYTE *startmsg = (UBYTE *) \
  413. "NoDelete V2.01 © HooverSoft 1992.\
  414.  This Program is \033[1mFREEWARE\033[0m.\n\
  415. Commercial distribution is strictly \033[4mprohibited.\033[0m\n";
  416.    long class, code;
  417.     long i;
  418.  
  419.     if(! (ArpBase = (struct ArpBase *) OpenLibrary("arp.library",0L) ) )
  420.         exit(FALSE);
  421.  
  422.     DosBase = (struct Library *) ArpBase->DosBase;
  423.     if(  (nd_msgport = (struct MsgPort *) FindPort("nd_port")) )
  424.         {
  425.         Printf("NoDelete already installed.\n");
  426.         CloseLibrary(ArpBase);
  427.         exit(0L);
  428.         }
  429.     else
  430.         nd_msgport = CreatePort("nd_port",0L);
  431.  
  432.  
  433.     IntuitionBase = (struct IntuitionBase *) ArpBase->IntuiBase;
  434.  
  435.     if(DosBase->lib_Version >= 36)
  436.         {
  437. #ifdef DEBUG
  438.         Printf("Using Kick 2.0, eh?\n");
  439. #endif
  440.         IS2_0=1;
  441.         }
  442.  
  443.     strcpy(title, (UBYTE *) what);
  444.  
  445.     SetTaskPri((struct Task *)FindTask(0L), -5L); /* set def. priority */
  446.     for(i=1; i<argc; i++)
  447.         {
  448. #ifdef DEBUG
  449.             Printf("Scanning argv[%ld]: %s\n", i, (UBYTE *) argv[i] );
  450. #endif
  451.             if( (char) argv[i][0] == '-')
  452.                 {
  453.                 switch( (char) argv[i][1] )
  454.                     {
  455.                     case 'y':    if( atoi(argv[i+1]) <= 245L)
  456.                                         mywindow.TopEdge = atoi(argv[i+1]);
  457.                                     i++;
  458.                                     goto go_away;
  459.                                      break;
  460.  
  461.                     case 'p':    if(atoi(argv[i+1]) < 10L && atoi(argv[i+1]) > -20L)
  462.                                         SetTaskPri((struct Task *)FindTask(0L),\
  463.                                                         (long)atoi(argv[i+1]));
  464.                                     i++;
  465.                                     goto go_away;
  466.                                     break;
  467.  
  468.  
  469.                     case  'O':    OVERRIDE = 1L;
  470.                                     goto go_away;
  471.                                     break;
  472.  
  473.                     case  'S':    SILENT = 1L;
  474.                                     goto go_away;
  475.                                     break;
  476.  
  477.                     case default:
  478.                                     Printf("Unknown Option %s\n", argv[i]);
  479.                                     quit("");
  480.                                     break;
  481.                     }
  482.                 /* end switch */
  483.                 }/*end if */
  484.             else
  485.                 {
  486.                 PAT_SET = 1L;
  487.                 if( howmany<50 )
  488.                     {
  489.                     patterns[howmany] = (char *) AllocMem(strlen(argv[i])+1L,\
  490. MEMF_CLEAR);
  491.                     if(!patterns[howmany])
  492.                         quit("Could not allocate pattern memory!\n");
  493.                     strcpy(patterns[howmany], argv[i]);
  494.                     }
  495.  
  496. #ifdef    DEBUG
  497.                 Printf("Copied %s, Result: %s\n", argv[i], patterns[i-1L]);
  498. #endif
  499.                 howmany++;
  500.                 }
  501. go_away:
  502.             ;
  503.         /*end for */
  504.             }
  505. #ifdef    DEBUG
  506.         Printf("I found %ld patterns.\n", howmany);
  507. #endif
  508.  
  509.     if(howmany)
  510.         strcat(title, (UBYTE *) " PAT SET");
  511.     else
  512.         {
  513.         PAT_SET = 0L;
  514.         strcat(title, (UBYTE *) " NO PAT");
  515.         }
  516.  
  517. /* calculate dimension of     TitleBar */
  518.  
  519.     mywindow.Width =  strlen(title) * 8L + 87L;
  520.     mywindow.LeftEdge = (640L-mywindow.Width) / 2L;
  521.  
  522.     mw = OpenWindow(&mywindow);
  523.     if(!mw)
  524.         quit("Could not open Window.\n\n");
  525.     if(!SILENT)
  526.         Printf(startmsg);
  527.  
  528.     deletefile=SetFunction(DosBase, -72L, &NoDelete);
  529.  
  530.     for(;;)
  531.         {
  532.         Wait(1L << mw->UserPort->mp_SigBit);
  533. getmsg:
  534.         while( (msg = GetMsg(mw->UserPort)))
  535.             {
  536.             class=msg->Class;
  537.          code=msg->Code;
  538.             ReplyMsg(msg);
  539.             if ((class == CLOSEWINDOW))
  540.             if(!ACTIVE)
  541.                     quit("");
  542.             if(class == VANILLAKEY)
  543.                 {
  544.                 switch(tolower(code) )
  545.                     {
  546.                     case ' ':
  547.                         if(PAT_SET)
  548.                             {
  549.                             strcpy(titlebak, (UBYTE *) "Pat: ");
  550.                             for(i=0L; i < howmany ; i++)
  551.                                 {
  552.                                 strcat(titlebak, patterns[i]);
  553.                                 strcat(titlebak, " ");
  554.                                 }
  555.                             titlebak[strlen(titlebak)-1L] = '\0';
  556.                             SetWindowTitles(mw, titlebak);
  557.                             Wait(1L << mw->UserPort->mp_SigBit);
  558.                             SetWindowTitles(mw, title);
  559.                             ReplyMsg( GetMsg(mw->UserPort) );
  560.                             }
  561.                         break;
  562.  
  563.                     case 'e': /* change patterns */
  564.                         edit_patterns();
  565. #ifdef DEBUG
  566.                         print_info();
  567. #endif
  568.                             PAT_SET = (howmany) ? 1 : 0;
  569.  
  570.                         if(PAT_SET)
  571.                             {
  572.                           strcpy(title, what);
  573.                           strcat(title, " PAT SET");
  574.                             }
  575.                         else
  576.                             {
  577.                             strcpy(title, what);
  578.                           strcat(title, " NO PAT");
  579.                           }
  580.                         SetWindowTitles(mw, title);
  581.                         break;
  582.  
  583.                     case 'c': /* clear patterns */
  584.                         for(i=0; i<howmany; i++)
  585.                             {
  586.                             FreeMem(patterns[i], strlen(patterns[i])+1L);
  587.                             patterns[i]=(char *) NULL;
  588.                             }
  589. #ifdef DEBUG
  590.                         print_info();
  591. #endif
  592.                         strcpy(title, what);
  593.                          strcat(title, " NO PAT");
  594.                         SetWindowTitles(mw, title);
  595.                         howmany=PAT_SET=0L;
  596.                         break;
  597.  
  598.                     case 'q': /* shorthand for quit */
  599.                             if(!ACTIVE)
  600.                                 quit("");
  601.                         break;
  602.  
  603.                     case default:
  604.                         break;
  605.                     }
  606.                 }
  607.  
  608.             else
  609.                 continue;
  610.             }
  611.         }
  612.     }
  613. #endif
  614.  
  615.